﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TLL.Log
{
    public class LogTime
    {
        private int hour = 0;
        private int minutes = 0;
        private int second = 0;

        public int Hour { get { return this.hour; } }
        public int Minutes { get { return this.minutes; } }
        public int Second { get { return this.second; } }
        public static LogTime Empty { get { return new LogTime(); } }

        public LogTime() { }
        public LogTime(int Second)
        {
            this.hour = Second / 60 / 60;
            this.minutes = Second / 60 % 60;
            this.second = Second % 60;
        }
        public LogTime(int Hour, int Minutes, int Second)
        {
            this.hour = Hour;
            this.minutes = Minutes;
            this.second = Second;
        }
        public int GetSecond()
        {
            return Hour * 3600 + Minutes * 60 + Second;
        }
        public int GetMinutes()
        {
            return this.hour * 60 + this.minutes;
        }
        public void Add(LogTime time)
        {
            this.hour += time.Hour + (this.Minutes + time.Minutes) / 60;
            this.minutes = (this.Minutes + time.Minutes) % 60 + (this.Second + time.Second) / 60;
            this.second = (this.Second + time.Second) % 60;
        }
        public void Add(int second)
        {
            this.hour += (this.Minutes + second / 60) / 60;
            this.minutes = (this.Minutes + second / 60) % 60 + (this.Second + second) / 60;
            this.second = (this.Second + second) % 60;
        }
        public static LogTime Parse(string item)
        {
            var elements = item.Split(':');
            return new LogTime(int.Parse(elements[0]), int.Parse(elements[1]), 0);
        }
        public static string Formated(LogTime Item, FormatedTimeType Type = FormatedTimeType.Default)
        {
            switch (Type)
            {
                case FormatedTimeType.Width:
                    return string.Format("{0} : {1} : {2}",
                        Item.Hour.ToString().Length > 1 ? Item.Hour.ToString() : "0" + Item.Hour.ToString(),
                        Item.Minutes.ToString().Length > 1 ? Item.Minutes.ToString() : "0" + Item.Minutes.ToString(),
                        Item.Second.ToString().Length > 1 ? Item.Second.ToString() : "0" + Item.Second.ToString());
                default:
                    return string.Format("{0}:{1}:{2}",
                        Item.Hour.ToString().Length > 1 ? Item.Hour.ToString() : "0" + Item.Hour.ToString(),
                        Item.Minutes.ToString().Length > 1 ? Item.Minutes.ToString() : "0" + Item.Minutes.ToString(),
                        Item.Second.ToString().Length > 1 ? Item.Second.ToString() : "0" + Item.Second.ToString());
            }
        }
    }
}
